﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.OleDb;

namespace XQ.Framework.io
{
    /// <summary>
    ///Excel 的摘要说明
    /// </summary>
    public class ExcelHelper
    {
        public ExcelHelper()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        public DataTable ExcelToDataTable(string path)
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + path + "'; Extended Properties=Excel 8.0";
            OleDbConnection conn = new OleDbConnection(strConn);
            try
            {
                conn.Open();
                DataTable sheetNameTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                string excelSql = "select * from [" + sheetNameTable.Rows[0][2].ToString() + "]";
                OleDbDataAdapter adapter = new OleDbDataAdapter(excelSql, conn);
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                adapter.Dispose();
                return ds.Tables[0];
            }
            catch (Exception err)
            {
                throw new Exception(err.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
